/**
 * 
 */
package coop.tecso.core.test.handlers;

import org.org.usurper.handlers.IHandler;
import org.org.usurper.handlers.basic.AbstractSpecificPropertyHandler;
import org.org.usurper.model.HandledBeanProperty;
import org.org.usurper.model.HandledConstructorArg;

/**
 * @author daniel
 *
 */
public class SpecificSingletonHandler extends AbstractSpecificPropertyHandler {

    private Object reusableInstance;
    private IHandler wrappedHandler;

    /**
     * @param handler
     */
    public SpecificSingletonHandler(Class<?> handledClass, String handledField, IHandler handler) {
        super(handledClass, handledField);
        wrappedHandler = handler;
    }

    /**
     * @param object
     */
    public SpecificSingletonHandler(Class<?> handledClass, String handledField, Object object) {
        super(handledClass, handledField);
        reusableInstance = object;
    }

    /* (non-Javadoc)
     * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledBeanProperty)
     */
    @Override
    public Object handle(HandledBeanProperty handledBeanProperty) {
        if(reusableInstance != null) {
            return reusableInstance;
        }
        return wrappedHandler.handle(handledBeanProperty);
    }

    /* (non-Javadoc)
     * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledConstructorArg)
     */
    @Override
    public Object handle(HandledConstructorArg handledConstructorArg) {
        if (reusableInstance != null) {
            return reusableInstance;
        }
        return wrappedHandler.handle(handledConstructorArg);
    }

}
